package com.yingpai.fitness.dvp_2_0.main.fragment.home;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.view.XGridView;
import com.yingpai.fitness.dvp_2_0.common.view.statusbar.StatusView;
import com.yingpai.fitness.dvp_2_0.main.activity.dynamic.Dvp_2_1_DynamicDetailActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.dynamic.Dvp_2_1_PublishActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.dynamic.DynamiVideoActivity;
import com.yingpai.fitness.dvp_2_0.main.widget.staggered.StaggeredGridView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ASUS on 2019/2/22.
 */

public class Dvp_2_1_DynamicFragment extends CommonFragment implements OnRefreshLoadMoreListener {

    @BindView(R.id.status_view)
    StatusView statusView;
    //    @BindView(R.id.hot_topic)
//    XGridView hotTopic;
    @BindView(R.id.staggered)
    StaggeredGridView staggered;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    @BindView(R.id.title_bar)
    RelativeLayout title_bar;

    public static Dvp_2_1_DynamicFragment newInstance() {
        return new Dvp_2_1_DynamicFragment();
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dvp_2_1_dynamic_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        title_bar.setVisibility(View.GONE);
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

        View headview = View.inflate(context, R.layout.staggered_head_view, null);
        XGridView hotTopic = headview.findViewById(R.id.hot_topic);
        QuickAdapter<String> hotTopicAdapter = new QuickAdapter<String>(context, R.layout.item_hot_topic_list) {
            @Override
            protected void convert(BaseAdapterHelper helper, String item) {
            }
        };
        hotTopic.setAdapter(hotTopicAdapter);
        hotTopic.setFocusable(false);
        staggered.addHeaderView(headview);


        hotTopicAdapter.add("");
        hotTopicAdapter.add("");
        hotTopicAdapter.add("");
        hotTopicAdapter.add("");
        hotTopicAdapter.add("");
        hotTopicAdapter.add("");

        int length = 140;
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;//密度
        int gridviewWidth = (int) (hotTopicAdapter.getCount() * (length + 12) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
        hotTopic.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        hotTopic.setColumnWidth(itemWidth); // 设置列表项宽
        hotTopic.setHorizontalSpacing(24); // 设置列表项水平间距
        hotTopic.setStretchMode(GridView.NO_STRETCH);
        hotTopic.setNumColumns(hotTopicAdapter.getCount()); // 设置列数量=列表集合数
        hotTopicAdapter.notifyDataSetChanged();

        QuickAdapter<String> quickAdapter = new QuickAdapter<String>(context, R.layout.item_hot_dynamic_list) {
            @Override
            protected void convert(BaseAdapterHelper helper, String item) {
                ImageView imageView = helper.getView(R.id.photo);
                if (helper.getPosition() == 0) {
                    helper.setText(R.id.title, "导入之后不能用啊，StaggeredGridView cannot be resolved to a type StaggeredGridView不能定义变量啊，求解导入之后不能用啊，StaggeredGridView cannot be resolved");
                } else if (helper.getPosition() == 3) {
                    helper.setText(R.id.title, "导入之后不能用啊，StaggeredGridView cannot be resolved to a type StaggeredGridView");
                } else if (helper.getPosition() == 4) {
                    Glide.with(context)
                            .load("http://c.hiphotos.baidu.com/zhidao/pic/item/562c11dfa9ec8a1359aa88b6f103918fa0ecc030.jpg")
                            .into(imageView);
                } else {

                }
            }
        };


        staggered.setAdapter(quickAdapter);
        //position 从1开始
        staggered.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Logger.e("onItemClick:" + position);
                if (position == 1) {
                    startActivity(DynamiVideoActivity.class);
                }/*else if(position == 2){
                    startActivity(TestActivity.class);
                }*/ else {
                    startActivity(Dvp_2_1_DynamicDetailActivity.class);
                }
            }
        });

        staggered.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Logger.e("onScroll" + firstVisibleItem);

                if ((visibleItemCount > 0) && (firstVisibleItem == 0)) {
                    if (view.getChildAt(0).getTop() >= 0) {
                        refreshLayout.setEnableRefresh(true);
                    } else {
                        refreshLayout.setEnableRefresh(false);
                    }
                } else {
                    refreshLayout.setEnableRefresh(false);
                }

                if ((totalItemCount > 0) && (view.getLastVisiblePosition() == totalItemCount - 1)) {
                    if (view.getBottom() >= view.getChildAt(view.getChildCount() - 1).getBottom()) {
                        refreshLayout.setEnableLoadMore(true);
                    } else {
                        refreshLayout.setEnableLoadMore(false);
                    }
                } else {
                    refreshLayout.setEnableLoadMore(false);
                }
            }
        });

        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.add("");
        quickAdapter.notifyDataSetChanged();

        return view;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    @OnClick({R.id.camera})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.camera:
                startActivity(Dvp_2_1_PublishActivity.class);
                break;
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        statusView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.normal_status));
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        refreshLayout.finishLoadMore();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        refreshLayout.finishRefresh();
    }
}
